//
//  ViewController.swift
//  DJSwiftPager
//
//  Created by johnyv on 03/13/2017.
//  Copyright (c) 2017 johnyv. All rights reserved.
//

import UIKit
import DJSwiftPager

class ViewController: UIViewController {
    
    private var viewPager:DJViewPager!
    private var tableView:UITableView!
    private var collectionView:UICollectionView!
    private var webView:UIWebView!
    private var flow:UICollectionViewFlowLayout!
    fileprivate let collectionCellID = "CollectionViewCell"

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        configUI()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func configUI() {
        tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        
        flow = UICollectionViewFlowLayout()
        flow.minimumInteritemSpacing = 0
        flow.minimumLineSpacing = 0
        flow.scrollDirection = .vertical
        
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flow)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionCellID)
        
        webView = UIWebView()
        webView.loadRequest(URLRequest(url: URL(string: "https://www.baidu.com")!))
        
        let titles = ["菜单一","菜单二","菜单三"]
        let icons = [#imageLiteral(resourceName: "issueTree"), #imageLiteral(resourceName: "hollow_out_star_blue"), #imageLiteral(resourceName: "later_in-tne_chat")]
        let views = [tableView, collectionView, webView] as [Any]
        
        viewPager = DJViewPager.init(frame: CGRect.init(x: 0, y: 20, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 20),
                                     titles: titles,
                                     icons: icons,
                                     selectedIcons: nil,
                                     views: views)
        view.addSubview(viewPager)
        
        viewPager.tipsCountArray = [100,8,0]
        
        viewPager.block = { (viewPager, index) in
            switch index {
                case 0:
                    print("Menu1")
                case 1:
                    print("Menu2")
                case 2:
                    print("Menu3")
                default:
                    break
            }
        }
    }

    fileprivate func getRandomColor() -> UIColor {
        let hue = CGFloat(arc4random() % 256) / 256.0
        let saturation = CGFloat(arc4random() % 128) / 256.0 + 0.5
        let brightness = CGFloat(arc4random() % 128) / 256.0 + 0.5
        return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
    }
}

extension ViewController:UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellID = "TableViewCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellID)
        if cell == nil {
            cell = UITableViewCell.init(style: .value1, reuseIdentifier: cellID)
        }
        cell?.selectionStyle = .none
        cell?.imageView?.image = #imageLiteral(resourceName: "my_headportrait")
        cell?.textLabel?.text = "演示文字-\(indexPath.row)"
        cell?.detailTextLabel?.text = "v2.0"
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 40
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionCellID, for: indexPath)
        cell.backgroundColor = getRandomColor()
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.width,
                           height: 100)
    }
}
